package com.yang.test.controller;

import com.yang.test.domain.Answer;
import com.yang.test.domain.VO.QuestionVO;
import com.yang.test.domain.VO.ResultVO;
import com.yang.test.service.MbtiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api")
public class MbtiController {
    
    @Autowired
    private MbtiService mbtiService;
    
    @GetMapping("/questions")
    public ResponseEntity<?> getQuestions(@RequestParam(value = "type", defaultValue = "simple") String testType) {
        Instant start = Instant.now();
        log.info("开始获取题目, 类型: {}", testType);
        try {
            List<QuestionVO> questions = mbtiService.getQuestions(testType);
            
            log.info("获取题目完成, 类型: {}, 题目数量: {}, 总耗时: {}", 
                    testType, questions.size(), Duration.between(start, Instant.now()));
            
            return ResponseEntity.ok(questions);
        } catch (Exception e) {
            log.error("获取题目失败: {}", e.getMessage(), e);
            return ResponseEntity.internalServerError().body(Map.of("error", "获取题目失败"));
        }
    }
    
    @PostMapping("/submit")
    public ResponseEntity<?> submitAnswers(@RequestBody Answer answer,
                                         @RequestParam(value = "type", defaultValue = "simple") String testType) {
        Instant start = Instant.now();
        log.info("开始处理答案提交请求");
        
        try {
            String resultId = mbtiService.processAnswers(answer.getAnswers(), testType);
            
            log.info("答案提交处理完成, 结果ID: {}, 总耗时: {}", 
                    resultId, Duration.between(start, Instant.now()));
            
            return ResponseEntity.ok(Map.of("resultId", resultId));
        } catch (Exception e) {
            log.error("处理答案提交请求失败: {}", e.getMessage(), e);
            return ResponseEntity.internalServerError().body(Map.of("error", "处理请求失败"));
        }
    }
    
    @GetMapping("/result/{id}")
    public ResponseEntity<?> getResult(@PathVariable("id") String resultId) {
        Instant start = Instant.now();
        log.info("开始获取结果, ID: {}", resultId);
        
        try {
            ResultVO result = mbtiService.getResult(resultId);
            
            if (result == null) {
                return ResponseEntity.notFound().build();
            }
            
            log.info("获取结果完成, ID: {}, 类型: {}, 总耗时: {}", 
                    resultId, result.getType(), Duration.between(start, Instant.now()));
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("获取结果失败: {}", e.getMessage(), e);
            return ResponseEntity.internalServerError().body(Map.of("error", "获取结果失败"));
        }
    }
}